/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oalam.bookstore.facade;

import com.oalam.bookstore.domain.accounts.Account;
import com.oalam.bookstore.domain.accounts.AccountCreationException;
import com.oalam.bookstore.domain.accounts.AccountLoginException;
import com.oalam.bookstore.domain.accounts.AccountLookupException;
import com.oalam.bookstore.domain.accounts.AccountManager;
import com.oalam.bookstore.domain.books.Book;
import com.oalam.bookstore.domain.books.Catalog;
import com.oalam.bookstore.domain.shopping.ShoppingCartItem;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.management.ManagementFactory;
import javax.management.ObjectName;
import javax.management.JMException;
import com.oalam.bookstore.facade.BookstoreFacadeStats;

/**
 *
 * @author Administrateur
 */
public class BookstoreFacade {

    private static final BookstoreFacadeStats mbeanStats = new BookstoreFacadeStats();
    public final AccountManager accMgr = AccountManager.getInstance();
    public final Catalog bookCatalog = Catalog.getInstance();

    public Account onCreateAccount(String name, String email, String pwd, String pwd2) {
        Account newAccount = null;
        try {
            boolean isValidQuery = accMgr.validateQuery(email, pwd, pwd2, name);
            if (isValidQuery) {
                newAccount = accMgr.createAccount(email, pwd, name);
            }

        } catch (AccountLookupException ex) {
            Logger.getLogger(BookstoreFacade.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccountCreationException ex) {
            Logger.getLogger(BookstoreFacade.class.getName()).log(Level.SEVERE, null, ex);
        }

        return newAccount;
    }

    public Account onLogin(String email, String pwd) {
        Account loggedAccount = null;
        try {
            loggedAccount = accMgr.login(email, pwd);
        } catch (AccountLoginException ex) {
            Logger.getLogger(BookstoreFacade.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccountLookupException ex) {
            Logger.getLogger(BookstoreFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return loggedAccount;

    }

    public Book[] onSearchBooks(String phrase, boolean doSearchByTitle) {

        List<Book> result = bookCatalog.findBooks(phrase, doSearchByTitle);

        return (Book[]) result.toArray(new Book[result.size()]);

    }

    public void onAddBookToShoppingCart(Account a, Book b, int quantity) {
        for (int i = 0; i < quantity; i++) {
            a.getShoppingCart().addItem(b);
        }
        a.getShoppingCart().calculateTotal("0");


    }

    public static void main(String[] args) throws IOException {
        boolean ok = true;
        java.io.StreamTokenizer input = new java.io.StreamTokenizer(System.in);


        try { // Register MBean in Platform MBeanServer
            ManagementFactory.getPlatformMBeanServer().
                    registerMBean(mbeanStats,
                    new ObjectName("com.steria.bookstore.facade:type=BookstoreFacadeStats"));
        } catch (JMException ex) {
            // TODO handle exception
        }

        while (ok) {
            input.nextToken();
            String str = input.sval;

            mbeanStats.addAccount();

            if (str.equals("0")) {
                ok = false;
            }
        }
    }
}
